﻿using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;
using CustomListViewAdapter.Adapters;
using CustomListViewAdapter.Models;
using System;
using System.Collections.Generic;
using System.Linq;

namespace CustomListViewAdapter
{
    [Activity(Label = "http://www.reachsos.com", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);

            var nameSearchEditText = FindViewById<EditText>(Resource.Id.NameSearchEditText);
            nameSearchEditText.TextChanged += (s, e) => ((IFilterable)FindViewById<ListView>(Resource.Id.UsersListView).Adapter).Filter.InvokeFilter(string.Empty);

            var users = GetUsers();
            var usersListView = FindViewById<ListView>(Resource.Id.UsersListView);
            usersListView.Adapter = new ListViewAdapter<User>(users, GetUserView, GetFilteredUsers);
        }

        private List<User> GetFilteredUsers(List<User> originalItems)
        {
            var nameSearchEditText = FindViewById<EditText>(Resource.Id.NameSearchEditText);
            if (!string.IsNullOrWhiteSpace(nameSearchEditText.Text))
            {
                return originalItems.Where(x =>
                    x.Name.StartsWith(nameSearchEditText.Text, StringComparison.InvariantCultureIgnoreCase) &&
                    x.Age >= 45).ToList();
            }

            return originalItems;
        }

        private View GetUserView(User user, View convertView, ViewGroup parent)
        {
            View view;

            if (user.Age >= 65)
            {
                view = LayoutInflater.Inflate(Resource.Layout.OldUserDataTemplate, parent, false);
                view.FindViewById<TextView>(Resource.Id.UserNameTextView).Text = user.ListName;
                view.FindViewById<TextView>(Resource.Id.UserAddressTextView).Text = user.Address;
                view.FindViewById<TextView>(Resource.Id.UserBonusTextView).Text = ((user.Age - 65) * 1000).ToString();
            }
            else
            {
                view = LayoutInflater.Inflate(Resource.Layout.UserDataTemplate, parent, false);
                view.FindViewById<TextView>(Resource.Id.UserNameTextView).Text = user.ListName;
                view.FindViewById<TextView>(Resource.Id.UserAddressTextView).Text = user.Address;
            }

            return view;
        }

        private List<User> GetUsers()
        {
            var random = new Random();
            return new List<User>{
                new User { Id = 1, Age = random.Next(9,81), Name= "Shoshana L. Robinson", Address= "Ap #691-1861 Fringilla St."},
                new User { Id = 2, Age = random.Next(9,81), Name= "Hadassah T. Lawrence", Address = "270-9592 Dui Street"},
                new User { Id = 3, Age = random.Next(9,81), Name= "Caleb R. Rios", Address = "375-1704 Feugiat Street"},
                new User { Id = 4, Age = random.Next(9,81), Name= "Keaton K. Booth", Address = "Ap #717-1834 Erat. Rd."},
                new User { Id = 5, Age = random.Next(9,81), Name= "Laura C. Maynard", Address = "Ap #893-343 Interdum Road"},
                new User { Id = 6, Age = random.Next(9,81), Name= "Cailin A. Mcleod", Address = "Ap #484-5309 Augue Road"},
                new User { Id = 7, Age = random.Next(9,81), Name= "Chava P. Wallace", Address = "693-5603 Donec St."},
                new User { Id = 8, Age = random.Next(9,81), Name= "Isabella I. Bush", Address = "Ap #100-9976 Donec Road"},
                new User { Id = 9, Age = random.Next(9,81), Name= "Leah Y. Acevedo", Address = "9389 Pede. Avenue"},
                new User { Id = 10, Age = random.Next(9,81), Name= "McKenzie N. Owen", Address = "453-5024 Et Road"},
                new User { Id = 11, Age = random.Next(9,81), Name= "Ivor Q. Woodard", Address = "P.O. Box 818, 8492 Eleifend Avenue"},
                new User { Id = 12, Age = random.Next(9,81), Name= "Winifred J. Clark", Address = "P.O. Box 760, 2645 Mauris St."},
                new User { Id = 13, Age = random.Next(9,81), Name= "Chaney M. Moss", Address = "970-5780 Sagittis Avenue"},
                new User { Id = 14, Age = random.Next(9,81), Name= "Justina R. Barr", Address = "P.O. Box 968, 5665 Non, St."},
                new User { Id = 15, Age = random.Next(9,81), Name= "Luke E. Orr", Address = "Ap #373-393 Vulputate Street"},
                new User { Id = 16, Age = random.Next(9,81), Name= "Thor G. Baxter", Address = "Ap #930-8980 Lacinia St."},
                new User { Id = 17, Age = random.Next(9,81), Name= "Moana T. Rodriquez", Address = "Ap #826-8981 Proin St."},
                new User { Id = 18, Age = random.Next(9,81), Name= "Aphrodite V. Dominguez", Address = "371-8128 Vulputate Rd."},
                new User { Id = 19, Age = random.Next(9,81), Name= "Sade C. Cameron", Address = "8680 Praesent Street"},
                new User { Id = 20, Age = random.Next(9,81), Name= "Chaney T. Cooper", Address = "Ap #758-7491 Class St."},
                new User { Id = 21, Age = random.Next(9,81), Name= "Lydia W. Cameron", Address = "152-3416 Mi St."},
                new User { Id = 22, Age = random.Next(9,81), Name= "Merritt C. Guzman", Address = "Ap #902-2633 Pulvinar Street"},
                new User { Id = 23, Age = random.Next(9,81), Name= "Shaeleigh D. Rowland", Address = "851-9396 Praesent Street"},
                new User { Id = 24, Age = random.Next(9,81), Name= "Alika Q. Baird", Address = "P.O. Box 982, 6859 Ligula. Av."},
                new User { Id = 25, Age = random.Next(9,81), Name= "Bertha M. Burgess", Address = "Ap #297-6268 Aliquam St."},
                new User { Id = 26, Age = random.Next(9,81), Name= "Gareth I. Robbins", Address = "468 Taciti St."},
                new User { Id = 27, Age = random.Next(9,81), Name= "Hillary H. Johns", Address = "Ap #768-1660 Integer Road"},
                new User { Id = 28, Age = random.Next(9,81), Name= "Quincy T. Whitehead", Address = "1325 Vestibulum Rd."}
            };
        }
    }
}